package com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper;

import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author xiaoxu
 * @since 2022-12-27
 */
@Component
public interface SysUserMapper extends BaseMapper<SysUser> {
//    查询用户名称，模糊查询，返回第一条，用于新增用户时，判断用户名称最后一位是否是数字，如果是数字，自动加1
@Select({
        "SELECT USER_NAME",
        "FROM (",
        "   SELECT USER_NAME, SUBSTRING_INDEX(SUBSTRING_INDEX(USER_NAME, #{userName}, -1), '.', -1) AS num_part",
        "   FROM sys_user",
        "   WHERE USER_NAME LIKE CONCAT(#{userName}, '%') AND USER_NAME REGEXP '[0-9]+'",
        ") AS subquery",
        "ORDER BY CAST(num_part AS UNSIGNED) DESC",
        "LIMIT 1"
})
String getLatestUserName(@Param("userName") String userName);
}
